layui.use('layer', function () {
    //由于layer弹层依赖jQuery，所以可以直接得到
    var $ = layui.$,
        layer = layui.layer;
    layui.use('element', function () {
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    });

    // 设置弹窗登录框
    layui.use('layer', function () { //独立版的layer无需执行这一句
        var $ = layui.jquery,
            layer = layui.layer; //独立版的layer无需执行这一句


    });
    //表单
    layui.use('form', function () {
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
    //富文本编辑器
    layui.use('layedit', function () {
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '' //接口url
                ,
                type: '' //默认post
            }
        });
        var index = layedit.build('demo', {
            height: 600,
            tool: ['strong' //加粗
                , 'italic' //斜体
                , 'underline' //下划线
                , 'del' //删除线

                , '|' //分割线

                , 'left' //左对齐
                , 'center' //居中对齐
                , 'right' //右对齐
                , 'link' //超链接
                , 'unlink' //清除链接
                , 'face' //表情
                , 'image' //插入图片
                , 'code' //代码
            ]
        }); //建立编辑器
        //发布blog
        var ca = $("#blogcont");
        var url = '';
        if (ca.length > 0) {
            ca = ca.val();
            $("#blog-class").val(ca);
            layui.form.render();
            url = '/Blog/blogupdate/' + $("#blogid").val();
        } else {
            url = '/Blog/blogrelease';
        }
        $("#but").click(function () {
            var content = layedit.getContent(index);
            var title = $(".blog-edit-head input").val();
            var col = $("#blog-class option:selected").val();
            var li = "";
            $(".mark_selection_list ul li").each(function () {
                li += $(this).find("span").text() + ",";
            });
            var op = $(".blog-edit-item input[type='radio']:checked").val();
            // console.log(title);
            // console.log(content);
            // console.log(li);
            // console.log(col);
            // console.log(op);
            $.ajax({
                url: url,
                type: "post",
                data: {title: title, content: content, li: li, col: col, op: op},
                success: function (data) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        window.location.href = "/Blog/blogindex";
                    }, 1000)
                }
            });
        });

        //
    });


    //标题计数
    $(".blog-edit-head").find("input").bind("input propertychange", function () {
        var text = $(this).val();
        var num = $(this).val().length;
        $(".blog-edit-head").find("span").html(num + "/100");
        if (num > 100) {
            layer.msg('超出字数限制了！');
            var lenText = text.substring(0, 100);
            $(this).val(lenText);
            $(".blog-edit-head").find("span").html(num - 1 + "/100");
        }
    });

    //博客标签
    // $(".mark_selection_list").hide();
    $(".blog-labels").find("span").click(function () {
        if ($(".is-selected").length < 5) {
            if ($(this).hasClass("is-selected") == false) {
                $(this).addClass("is-selected");
                $(".mark_selection_list").find("ul").append('<li><span>' + $(this).text() +
                    '</span><i class="layui-icon">&#x1007;</i></li>');
            }
        }
        $(".mark_selection_list").find("i").click(function () {
            var isSelected = $(this).prev().text();
            var allSelsct = $(".blog-labels").find("span");
            allSelsct.each(function () {
                if ($(this).text() == isSelected) {
                    $(this).removeClass("is-selected");
                }
            });
            $(this).parent().remove();
        });
    });
    $(".mark_selection_list").find("i").click(function () {
        var isSelected = $(this).prev().text();
        var allSelsct = $(".blog-labels").find("span");
        allSelsct.each(function () {
            if ($(this).text() == isSelected) {
                $(this).removeClass("is-selected");
            }
        });
        $(this).parent().remove();
    });


    // $(".mark_selection_list").find("i").click(function() {
    // 	$(this).parent().remove();
    // });


    $(".mark_selection_box").click(function (e) {
        // $(".blog-labels ").show();
        e.stopPropagation();
        if ($(".blog-labels").is(":hidden")) {
            $(".blog-labels ").show();
        } else {
            $(".blog-labels").hide();
        }
        $(document).click(function () {
            if ($(".blog-labels").is(":visible")) {
                $(".blog-labels").hide();
            }
        });
    });
    $(".label-icon").click(function () {
        $(".blog-labels").hide();
    });

});
